/**
 * @Date 2023-02-09 17:14:09
 * @Remark
 */

// config
import { ROUTE_MAIN_VIEW } from "project/config";
// state
import { makeAutoObservable, toJS } from "mobx";
// script & methods & public
// 其它

class AppState {
  collapsed = false; // 侧边栏折叠
  isFull = false; // 全屏
  open: string[] = []; // 打开的
  selected: string[] = []; // 选中的
  tempSelected: string[] = []; // 临时选中的
  tagGroup: AppMenu[] = []; // 顶部tag组
  constructor() {
    makeAutoObservable(this);
  }
  collapsedChange() {
    this.collapsed = !this.collapsed;
    if (this.collapsed) {
      this.tempSelected = toJS(this.open);
    } else {
      setTimeout(() => {
        this.open = toJS(this.tempSelected);
        // storageUtils.setStore(LOCAL_MENU_OPEN, this.getOpen(), false);
      }, 50);
    }
  }
  setFull(val: boolean) {
    this.isFull = val;
  }
  setOpen(val: string[]) {
    if (this.collapsed && val.length > 0) {
      this.tempSelected = val;
    }
    this.open = val;
  }
  addOpen(val: string) {
    // 如果有就不再次添加
    const flag = this.open.some((el) => el === val);
    if (flag) return;
    // 根据菜单是否收缩处理
    if (this.collapsed) {
      this.tempSelected.push(val);
    } else {
      this.open.push(val);
    }
  }
  getOpen() {
    return toJS(this.open);
  }
  setSelected(val: string[]) {
    this.selected = val;
  }
  getSelected() {
    return toJS(this.selected);
  }
  getTagGroup() {
    return toJS(this.tagGroup);
  }
  addTag(menu: AppMenu | undefined) {
    if (!menu) return;
    if (this.tagGroup.some((el) => el.key === menu.key)) {
      return;
    }
    this.tagGroup.push(menu);
  }
  delTag(route: AppMenu) {
    const newList = toJS(this.tagGroup).filter((el) => el.key !== route.key);
    this.tagGroup = newList;
  }
  delTagOther(route?: AppMenu) {
    if (route) {
      const newList = toJS(this.tagGroup).filter(
        (el) => el.key === route.key || el.key === ROUTE_MAIN_VIEW,
      );
      this.tagGroup = newList;
    } else {
      const newList = toJS(this.tagGroup).filter(
        (el) => el.key === ROUTE_MAIN_VIEW,
      );
      this.tagGroup = newList;
    }
  }
}

export default AppState;
